import CryptoJS from "crypto-js"; //引用AES源码js
import moment from 'moment';
// /**
//  * 解密
//  * @param encryptedBase64Str
//  * @returns {any}
//  */
export function aseDecode(encryptedBase64Str) {
  var key = CryptoJS.enc.Utf8.parse('abcdefgabcdefg12');
  encryptedBase64Str = encryptedBase64Str.replace(/[\r\n]/g, "");
  var decrypted = CryptoJS.AES.decrypt(encryptedBase64Str, key, {
    mode: CryptoJS.mode.ECB,
    padding: CryptoJS.pad.Pkcs7
  });
  var jsonStr = CryptoJS.enc.Utf8.stringify(decrypted)
  jsonStr = jsonStr.indexOf('}') > -1 ? jsonStr.substring(0, jsonStr.indexOf('}') + 1) : "{}";
  return JSON.parse(jsonStr)
}


// /**
//  * 加密
//  * @param encryptedBase64Str
//  * @returns {any}
//  */

export function aesEncode(word) {
  var key = CryptoJS.enc.Utf8.parse("abcdefgabcdefg12");
  var srcs = CryptoJS.enc.Utf8.parse(word);
  var encrypted = CryptoJS.AES.encrypt(srcs, key, { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 });
  return encrypted.toString();
}


//下载
export function downloadFile(data) {
  let fileName = localStorage.getItem('jmFileName')
  const elink = document.createElement("a");
  elink.download = fileName ||'名册导入模板.xlsx';
  elink.style.display = "none";
  elink.href = URL.createObjectURL(data);
  document.body.appendChild(elink);
  elink.click();
  URL.revokeObjectURL(elink.href); // 释放URL 对象
  document.body.removeChild(elink);

}


export function getLastDayOfMonth(year, month) {
  const lastDay = new Date(year, month + 1, 0).getDate()
  return lastDay
}
export function formatDate(value, format = 'YYYY-MM-DD') {
  if (moment(value).isValid) {
      let date = moment(value).format(format);
      if (date == 'Invalid date') {
          return value;
      } else {
          return date;
      }
  } else {
      return value;
  }
}

export function currentDateArr() {
  var currentDate = new Date();
  var year = currentDate.getFullYear(); // 获取当前年份
  var month = currentDate.getMonth() + 1; // 获取当前月份（注意月份是从0开始，所以要加1）
  return [year + '-01', year + '-' + month]
}

//判断object为空
export function isObjEmpty(obj) {
  return Object.keys(obj).length === 0;
}